<?php

/**
 * This is the model class for table "news".
 *
 * The followings are the available columns in table 'news':
 * @property integer $id
 * @property string $name
 * @property string $short
 * @property string $text
 * @property string $date
 * @property string $title
 * @property string $keywords
 * @property string $descriptions
 * @property integer $status
 */
class News extends CActiveRecord
{
	const STATUS_NEW = 0;
	const STATUS_PUBLIC = 1;
	const STATUS_DELETE = 5;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'news';
	}

	public static function get_array_status(){
		return array(
			self::STATUS_NEW => Yii::t('main', 'Не опубліковано'),
			self::STATUS_PUBLIC => Yii::t('main', 'Опубліковано'),
			self::STATUS_DELETE => Yii::t('main', 'Видалено'),
		);
	}

	public static function get_status_text($status=0){
		$array=self::get_array_status();

		if(!empty ($array[$status])){
			return $array[$status];
		}else{
			return FALSE;
		}
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('status', 'numerical', 'integerOnly'=>true),
			array('date, name', 'required'),
			array('date', 'type', 'type' => 'date', 'message' => '{attribute}:  не правильний формат дати!', 'dateFormat' => 'yyyy-MM-dd'),
			array('name, short, title, keywords, descriptions', 'length', 'max'=>250),
			array('text', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, short, text, date, title, keywords, descriptions, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Назва новини',
			'short' => 'Короткий опис',
			'text' => 'Текс',
			'date' => 'Дата',
			'title' => 'SEO_title',
			'keywords' => 'SEO_Keywords',
			'descriptions' => 'SEO_Descriptions',
			'status' => 'Статус',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('short',$this->short,true);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('status',$this->status);


//		$sort= new CSort;
//		$sort->attributes=array(
//			'id'=>array(
//				'asc'=>'id',
//				'desc'=>'id desc',
//			),
//			'name'=>array(
//				'asc'=>'name',
//				'desc'=>'name desc',
//			),
//			'date'=>array(
//				'asc'=>'date',
//				'desc'=>'date desc',
//			),
//		);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
//			'pagination'=>array(
//					'pageSize'=>3,
//				),
	//		'sort'=>$sort,
		));
	}
}